const path = require("path");
const { smart } = require("webpack-merge");
const base = require("./webpack.base");
const TerserWebpackPlugin = require("terser-webpack-plugin");
const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin");
const webpack = require("webpack");
const glob = require('glob');
const PurgeCssWebpackPlugin = require('purgecss-webpack-plugin');
module.exports = smart(base, {
  mode: "production",
  optimization: {
    //这里放着优化的内容
    minimize: true,
    minimizer: [
      //表示放优化的插件
      new TerserWebpackPlugin({
        parallel: true, //开启多进程并行压缩
        cache: true //开启缓存
      }),
      new OptimizeCssAssetsWebpackPlugin({
        assetNameRegExp: /\.css$/g, //指定要压缩的模块的正则
        //cssnano是PostCSS的CSS优化和分解插件。cssnano采用格式很好的CSS，并通过许多优化，以确保最终的生产环境尽可能小。
        cssProcessor: require("cssnano")
      })
    ]
  },
  module: {
    rules: [
      {
        test: /\.(gif|png|jpe?g|svg|bmp)$/i,
        use: [
          {
            //url内部内置了file-loader
            loader: "url-loader",
            options: {
              //如果要加载的图片大小小于10K的话，就把这张图片转成base64编码内嵌到html网页中去
              limit: 0,
              //要把图片拷贝到images目录下
              outputPath: "images",
              publicPath: "/images"
            }
          },
          {
            loader: "image-webpack-loader",
            options: {
              mozjpeg: {
                progressive: true,
                quality: 65
              },
              // optipng.enabled: false will disable optipng
              optipng: {
                enabled: false
              },
              pngquant: {
                quality: "65-90",
                speed: 4
              },
              gifsicle: {
                interlaced: false
              },
              // the webp option will enable WEBP
              webp: {
                quality: 75
              }
            }
          }
        ]
      }
    ]
  },
  plugins: [
    //会把这些变量和值挂到全局对象上
    new webpack.DefinePlugin({
      PRODUCTION: JSON.stringify(true),
      VERSION: "1",
      EXPRESSION: "1+2",
      COPYRIGHT: {
        AUTHOR: JSON.stringify("珠峰培训")
      }
    }),
    new PurgeCssWebpackPlugin({
      paths:glob.sync(`${path.join(__dirname,'src')}/**/*`,{nodir:true})
    })
  ]
});
